﻿using System;
using System.Windows.Forms;
using cndp.Model;

namespace cndp.View
{
    public partial class FormShowDivers : Form
    {
        private string _oldValue;
        private int _rowOfDiver;
        public bool Edit = true;

        public FormShowDivers()
        {
            InitializeComponent();
            dataGridViewContestDiverRegister.Columns["personalCodeNumberDataGridViewTextBoxColumn"].ReadOnly = true;
        }

        private void FormShowDivers_Load(object sender, EventArgs e)
        {
            if (!Edit)
            {
                dataGridViewContestDiverRegister.ReadOnly = true;
            }
            KeyPreview = true;
            if (dataGridViewContestDiverRegister.Rows.Count != 0)
            {
                _rowOfDiver = 0;
                dataGridViewContestDiverList_CellClick(dataGridViewContestDiverRegister,
                    new DataGridViewCellEventArgs(0, _rowOfDiver));
                dataGridViewContestDiverRegister.Rows[_rowOfDiver].Selected = true;
            }
        }

        private void dataGridViewContestDiverList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            _rowOfDiver = e.RowIndex;
        }

        private void dataGridViewContestDiverList_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            _oldValue = dataGridViewContestDiverRegister[e.ColumnIndex, e.RowIndex].Value.ToString();
        }

        private void dataGridViewContestDiverList_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            var d = new Datacheck();
            var row = dataGridViewContestDiverRegister[e.ColumnIndex, e.RowIndex];
            if (row.ColumnIndex == 0 || row.ColumnIndex == 1 || row.ColumnIndex == 4)
            {
                if (d.CheckIfContainNumbers(row.Value.ToString()))
                {
                    MessageBox.Show(@"Fel format.", @"Ogiltigt data.");
                    row.Value = _oldValue;
                }
            }
            if (row.ColumnIndex == 2)
            {
                if (!(row.Value.ToString() == "Herr" || row.Value.ToString() == "Dam"))
                {
                    MessageBox.Show(@"Fel format.", @"Ogiltigt data.");
                    row.Value = _oldValue;
                }
            }
            if (row.ColumnIndex == 3)
            {
                if (!d.CheckIfBirthDay(row.Value.ToString()))
                {
                    MessageBox.Show(@"Fel format.", @"Ogiltigt data.");
                    row.Value = _oldValue;
                }
            }
        }

        private void FormShowDivers_KeyDown(object sender, KeyEventArgs e)
        {
            _rowOfDiver = CheckWhichRowIsMarked();
            if (e.KeyData == Keys.Escape)
                Close();
            if (e.KeyCode == Keys.Delete)
                if (_rowOfDiver == (dataGridViewContestDiverRegister.Rows.Count - 1))
                {
                    dataGridViewContestDiverRegister.Rows.RemoveAt(_rowOfDiver);
                    _rowOfDiver--;
                    if (_rowOfDiver != -1)
                    {
                        dataGridViewContestDiverRegister.Rows[_rowOfDiver].Cells[0].Selected = true;
                    }
                }
                else
                {
                    dataGridViewContestDiverRegister.Rows.RemoveAt(_rowOfDiver);
                }


            if (e.KeyCode == Keys.Up)
                if ((_rowOfDiver != 0) && (_rowOfDiver != -1))
                {
                    _rowOfDiver--;
                }
            if (e.KeyCode == Keys.Down)
                if (_rowOfDiver != (dataGridViewContestDiverRegister.Rows.Count - 1))
                {
                    _rowOfDiver++;
                }
            if (e.KeyData == Keys.Enter)
                if (_rowOfDiver != dataGridViewContestDiverRegister.Rows.Count)
                {
                    _rowOfDiver++;
                }
        }

        private int CheckWhichRowIsMarked()
        {
            var selectedRow = dataGridViewContestDiverRegister.CurrentCell.RowIndex;
            return selectedRow;
        }
    }
}